package com.weiyi.mashangzhu.Landlord.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Base.BaseActivity;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Roomer.Activity.ChangeRealPwdActivity;
import com.weiyi.mashangzhu.Roomer.Activity.NewPwdActivity;
import com.weiyi.mashangzhu.Utils.LoadingDialog;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.weiyi.mashangzhu.Weight.PasswordEditText;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class GatheringActivity extends BaseActivity implements View.OnClickListener, PasswordEditText.PasswordFullListener {

    String account;
    ImageView delete, deletekeyboard;
    TextView one, two, three, four, five, six, seven, eight, nine, zero, forgot_pwd;
    PasswordEditText passwordEditText;
    String info, id;
    @BindView(R.id.price)
    TextView price;
    @BindView(R.id.deposit)
    TextView deposit;
    @BindView(R.id.et_deposit)
    EditText etDeposit;
    @BindView(R.id.et_content)
    EditText etContent;
    String account2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gathering);
        ButterKnife.bind(this);
        info = getIntent().getStringExtra("info");
//        account = getIntent().getStringExtra("account");
        id = getIntent().getStringExtra("id");
        LoadingDialog.show(GatheringActivity.this);
        OkHttpUtils.get()
                .addParams("token", SPUtils.get(GatheringActivity.this, "token", "") + "")
                .addParams("orderId", id)
                .url(API.BaseApi_L + API.FGathering)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        LoadingDialog.dismiss(GatheringActivity.this);
                        T.showShort(GatheringActivity.this, "数据获取失败，请稍后再试");

                    }

                    @Override
                    public void onResponse(String response) {
                        LoadingDialog.dismiss(GatheringActivity.this);
                        Log.e("00000000000000000000", "=" + response);
                        if (response != null) {
                            try {
                                JSONObject jsonObject = new JSONObject(response);
                                if (jsonObject.getInt("status") == 0) {
                                    JSONObject jsonObject1 = jsonObject.getJSONObject("data");
                                    price.setText(jsonObject1.getDouble("oPrice")+"");
                                    deposit.setText(jsonObject1.getDouble("oDeposit")+"");
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                    }
                });

    }

    public void showPopupWindow() {
        View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_input_keyboard, null);
        final PopupWindow popupWindow = new PopupWindow(contentView,
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setTouchable(true);
        popupWindow.setAnimationStyle(R.style.main_menu_animstyle);
        popupWindow.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.shape_pop_bg));
        deletekeyboard = (ImageView) contentView.findViewById(R.id.delete_dialog);
        delete = (ImageView) contentView.findViewById(R.id.delete);
        one = (TextView) contentView.findViewById(R.id.one);
        two = (TextView) contentView.findViewById(R.id.two);
        three = (TextView) contentView.findViewById(R.id.three);
        four = (TextView) contentView.findViewById(R.id.four);
        five = (TextView) contentView.findViewById(R.id.five);
        six = (TextView) contentView.findViewById(R.id.six);
        seven = (TextView) contentView.findViewById(R.id.seven);
        eight = (TextView) contentView.findViewById(R.id.eight);
        nine = (TextView) contentView.findViewById(R.id.nine);
        zero = (TextView) contentView.findViewById(R.id.zero);
        forgot_pwd = (TextView) contentView.findViewById(R.id.forgot_pwd);
        passwordEditText = (PasswordEditText) contentView.findViewById(R.id.password_edit_text);
        forgot_pwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //忘记密码
                Intent intent = new Intent(GatheringActivity.this, NewPwdActivity.class);
                intent.putExtra("info", "tixian_l");
                GatheringActivity.this.startActivity(intent);
            }
        });

        delete.setOnClickListener(this);
        one.setOnClickListener(this);
        two.setOnClickListener(this);
        three.setOnClickListener(this);
        four.setOnClickListener(this);
        five.setOnClickListener(this);
        six.setOnClickListener(this);
        seven.setOnClickListener(this);
        eight.setOnClickListener(this);
        nine.setOnClickListener(this);
        zero.setOnClickListener(this);
        passwordEditText.setOnPasswordFullListener(this);
        // 设置好参数之后再show
        if (popupWindow.isShowing()) {

        } else {
            popupWindow.showAsDropDown(contentView);
        }

        deletekeyboard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
    }

    @Override
    public void onClick(View v) {
        View clickView = v;
        if (clickView instanceof TextView) {
            // 如果点击的是TextView
            String number = ((TextView) clickView).getText().toString();
            if (!TextUtils.isEmpty(number)) {
                passwordEditText.addPassword(number);
            }
        } else if (clickView instanceof ImageView) {
            // 如果是图片那肯定点击的是删除
            passwordEditText.deleteLastPassword();
        }
    }

    @Override
    public void passwordFull(String password) {
        Log.e("ggggggggggggg",etContent.getText()+"");
        LoadingDialog.show(GatheringActivity.this);
        OkHttpUtils.get()
                .addParams("token", SPUtils.get(this, "token", "") + "")
                .addParams("orderId",id)
                .addParams("deductionNum", etDeposit.getText()+"")
                .addParams("deductionReason", etContent.getText()+"")
                .addParams("uAccountpw",password)
                .url(API.BaseApi_L + API.DoGathering)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        LoadingDialog.dismiss(GatheringActivity.this);
                        T.showShort(GatheringActivity.this, "数据获取失败，请稍后再试");
                    }

                    @Override
                    public void onResponse(String response) {
                        LoadingDialog.dismiss(GatheringActivity.this);
                        Log.e("wwwwwwwww", "==" + response);
                        try {
                            if (response != null) {
                                JSONObject jsonObject = new JSONObject(response);
                                T.showShort(GatheringActivity.this, jsonObject.getString("msg"));
                                if (jsonObject.getInt("status") == 0) {
                                    finish();
                                }
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
//
    }

    @OnClick({R.id.back, R.id.identity_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
//            case R.id.account:
//                final Intent intent = new Intent(this, SetAliAccountActivity.class);
//                startActivityForResult(intent, 0x001);
//                break;
            case R.id.identity_confirm:
//                if ((tvAccount.getText() + "").equals("")) {
//                    T.showShort(this, "请选择支付宝账号");
//                } else {
                    if (!(etDeposit.getText()+"").trim().equals("")){
                        //输入原因
                        if (Double.parseDouble(etDeposit.getText()+"")>Double.parseDouble(deposit.getText()+"")){
                            T.showShort(GatheringActivity.this, "扣除金额不能大于押金金额");
                        }else {
                            if ((etContent.getText() + "").equals("")) {

                                T.showShort(GatheringActivity.this, "请输入押金扣除原因");
                            } else {
                                //可以提交
                                LoadingDialog.show(this);
                                OkHttpUtils.get()
                                        .addParams("token", SPUtils.get(GatheringActivity.this, "token", "") + "")
                                        .url(API.BaseApi_L + API.GetPwd)
                                        .build()
                                        .execute(new StringCallback() {
                                            @Override
                                            public void onError(Request request, Exception e) {
                                                LoadingDialog.dismiss(GatheringActivity.this);
                                                T.showShort(GatheringActivity.this, "数据获取失败，请稍后再试");

                                            }

                                            @Override
                                            public void onResponse(String response) {
                                                LoadingDialog.dismiss(GatheringActivity.this);
                                                Log.e("pppppppppppp", "=" + response);
                                                if (response != null) {
                                                    try {
                                                        JSONObject jsonObject = new JSONObject(response);
                                                        if (jsonObject.getInt("status") == 0) {
                                                            if (jsonObject.getString("data") == null || jsonObject.getString("data").equals("null") || jsonObject.getString("data").equals("")) {
                                                                //无密码
                                                                Intent intent1 = new Intent(GatheringActivity.this, ChangeRealPwdActivity.class);
                                                                intent1.putExtra("info", "set");
                                                                GatheringActivity.this.startActivity(intent1);
//
                                                            } else {
                                                                //有密码
                                                                showPopupWindow();
                                                            }
                                                        }
                                                    } catch (JSONException e) {
                                                        e.printStackTrace();
                                                    }
                                                }

                                            }
                                        });
                            }
                        }
                    }else {
                        //可以提交
                        LoadingDialog.show(this);
                        OkHttpUtils.get()
                                .addParams("token", SPUtils.get(GatheringActivity.this, "token", "") + "")
                                .url(API.BaseApi_L + API.GetPwd)
                                .build()
                                .execute(new StringCallback() {
                                    @Override
                                    public void onError(Request request, Exception e) {
                                        LoadingDialog.dismiss(GatheringActivity.this);
                                        T.showShort(GatheringActivity.this, "数据获取失败，请稍后再试");

                                    }

                                    @Override
                                    public void onResponse(String response) {
                                        LoadingDialog.dismiss(GatheringActivity.this);
                                        Log.e("pppppppppppp", "=" + response);
                                        if (response != null) {
                                            try {
                                                JSONObject jsonObject = new JSONObject(response);
                                                if (jsonObject.getInt("status") == 0) {
                                                    if (jsonObject.getString("data") == null || jsonObject.getString("data").equals("null") || jsonObject.getString("data").equals("")) {
                                                        //无密码
                                                        Intent intent1 = new Intent(GatheringActivity.this, ChangeRealPwdActivity.class);
                                                        intent1.putExtra("info", "set");
                                                        GatheringActivity.this.startActivity(intent1);
//
                                                    } else {
                                                        //有密码
                                                        showPopupWindow();
                                                    }
                                                }
                                            } catch (JSONException e) {
                                                e.printStackTrace();
                                            }
                                        }

                                    }
                                });

                    }



//                }
                break;
        }
    }
}
